import Mock from 'mockjs'

const Random = Mock.Random

// 生成民族数据
const ethnicities = ['汉族', '满族', '蒙古族', '回族', '藏族', '维吾尔族', '苗族', '壮族', '朝鲜族', '彝族']

// 生成工作单位数据
const workUnits = [
  '中共中央党校',
  '省委党校',
  '市委组织部',
  '区委办公室',
  '街道党工委',
  '社区党支部',
  '机关第一党支部',
  '企业党委',
  '高校党委'
]

// 生成学生列表数据
const generateMemberList = (count = 100) => {
  const items = []
  for (let i = 0; i < count; i++) {
    const joinDate = Random.date('yyyy-MM-dd')
    items.push({
      id: Random.id(),
      name: Random.cname(),
      gender: Random.pick(['1', '2']), // 1-男，2-女
      ethnicity: Random.pick(ethnicities),
      birthDate: Random.date('yyyy-MM-dd'),
      joinDate: joinDate,
      phone: Mock.mock(/^1[3-9]\d{9}$/),
      workUnit: Random.pick(workUnits),
      status: Random.pick(['1', '2']), // 1-正常，2-停用
      createTime: Random.datetime(),
      updateTime: Random.datetime()
    })
  }
  return items
}

// Mock学生列表接口
Mock.mock(new RegExp('/api/party-member/list'), 'get', (options) => {
  const { url } = options
  const params = new URLSearchParams(url.split('?')[1])
  const page = parseInt(params.get('page')) || 1
  const limit = parseInt(params.get('limit')) || 20
  const name = params.get('name')
  const gender = params.get('gender')
  
  let items = generateMemberList()
  
  // 按条件筛选
  if (name) {
    items = items.filter(item => item.name.includes(name))
  }
  if (gender) {
    items = items.filter(item => item.gender === gender)
  }
  
  // 分页
  const start = (page - 1) * limit
  const end = start + limit
  
  return {
    code: 200,
    data: {
      total: items.length,
      items: items.slice(start, end)
    },
    message: 'success'
  }
})

// Mock获取学生详情接口
Mock.mock(new RegExp('/api/party-member/\\d+'), 'get', () => {
  return {
    code: 200,
    data: generateMemberList(1)[0],
    message: 'success'
  }
})

// Mock新增学生接口
Mock.mock('/api/party-member/add', 'post', (options) => {
  return {
    code: 200,
    data: JSON.parse(options.body),
    message: '添加成功'
  }
})

// Mock更新学生接口
Mock.mock(new RegExp('/api/party-member/\\d+'), 'put', (options) => {
  return {
    code: 200,
    data: JSON.parse(options.body),
    message: '更新成功'
  }
})

// Mock删除学生接口
Mock.mock(new RegExp('/api/party-member/\\d+'), 'delete', () => {
  return {
    code: 200,
    data: null,
    message: '删除成功'
  }
})

// Mock导入学生接口
Mock.mock('/api/party-member/import', 'post', () => {
  return {
    code: 200,
    data: null,
    message: '导入成功'
  }
})

// Mock导出学生接口
Mock.mock(new RegExp('/api/party-member/export'), 'get', () => {
  return {
    code: 200,
    data: generateMemberList(),
    message: '导出成功'
  }
}) 